// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
const Ymd = new Date()
exports.main = async(event, context) => {
  try {
    if (event.topicNumb < (event.maxStuNumb - 1)) {
      const a = await db.collection('Option').where({
        topicId: event.topicId,
        userId: _.neq(event.stuId)
      }).field({
        userId: true
      }).get()
      for (var i = 0; i < a.data.length; i++) {
        await db.collection('Message').add({
          data: {
            sendId: event.teaId,
            sendName: event.teaName,
            receiveId: a.data[i].userId,
            messageType: '课题申请失败',
            topicId: event.topicId,
            topicName: event.topicName,
            time: Date.parse(new Date()),
            dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate()
          }
        })
      }
      await db.collection('Message').add({
        data: {
          sendId: event.teaId,
          sendName: event.teaName,
          receiveId: event.stuId,
          messageType: '课题申请成功',
          topicId: event.topicId,
          topicName: event.topicName,
          time: Date.parse(new Date()),
          dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate()
        }
      })
      await db.collection('Topic').doc(event.topicId).update({
        data: {
          ktzt: 1,
          stuId: event.stuId,
          stuName: event.stuName
        }
      })
      await db.collection('Option').where(_.or([{
          userId: event.stuId
        },
        {
          topicId: event.topicId
        }
      ])).remove()
      await db.collection('Student').doc(event.stuId).update({
        data: {
          stuState: 1,
          stuTopicId: event.topicId,
          stuTopicName: event.topicName
        }
      })
    } else if (event.topicNumb == (event.maxStuNumb - 1)) {
      const a = await db.collection('Option').where({
        topicId: event.topicId,
        userId: _.neq(event.stuId)
      }).field({
        userId: true
      }).get()
      for (var i = 0; i < a.data.length; i++) {
        await db.collection('Message').add({
          data: {
            sendId: event.teaId,
            sendName: event.teaName,
            receiveId: a.data[i].userId,
            messageType: '课题申请失败',
            topicId: event.topicId,
            topicName: event.topicName,
            time: Date.parse(new Date()),
            dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate()
          }
        })
      }
      await db.collection('Message').add({
        data: {
          sendId: event.teaId,
          sendName: event.teaName,
          receiveId: event.stuId,
          messageType: '课题申请成功',
          topicId: event.topicId,
          topicName: event.topicName,
          time: Date.parse(new Date()),
          dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate()
        }
      })
      await db.collection('Topic').doc(event.topicId).update({
        data: {
          ktzt: 1,
          stuId: event.stuId,
          stuName: event.stuName
        }
      })
      await db.collection('Option').where(_.or([{
          userId: event.stuId
        },
        {
          topicId: event.topicId
        }
      ])).remove()
      await db.collection('Topic').where({
        ktzt: 0
      }).update({
        data: {
          ktzt: 2
        }
      })
      await db.collection('Student').doc(event.stuId).update({
        data: {
          stuState: 1,
          stuTopicId: event.topicId,
          stuTopicName: event.topicName
        }
      })
      await db.collection('Teacher').doc(event.stuId).update({
        data: {
          teaState: 1
        }
      })
    } else {
      await db.collection('Topic').where({
        ktzt: 0
      }).update({
        data: {
          ktzt: 2
        }
      })
    }
    return {
      result: 1
    }
  } catch (e) {
    return {
      result: 0
    }
  }
}